//= require lib/jquery.fancybox.pack

$(document).ready(function(){

    // -- Login page
   $("#content_wrapper").prepend("<img src='/osdd.png' style='float:left;margin:-28px 6px 0 0;' /><h1>OpenSDD</h1><br />");
   $("input[name=username]").focus();

   // -- Start new scan
   $("#new_scan .cancel").remove();
   var oldButton = $("#new_scan #scan_submit");
   var newButton = oldButton.clone();
       newButton.attr("value", "RUN SCAN");
       //newButton.attr("type", "button");
       newButton.attr("class", "submit_button");
       newButton.insertBefore(oldButton);
       
       oldButton.remove();

   var form_width = $("#main_content_wrapper").width() / 2;
   var button_width = $("#new_scan #scan_submit").width() / 2;
   $("#new_scan #scan_submit").css({"margin-left":form_width - button_width});
   $("#new_scan").append("<div id='scan_alert'><div id='scan_alert_msg'>Scanning the system...</div></div>");

   if($("table#systems"))
   {
      $("table#systems tr").each(function(elem){
        var tracker = $(this).find(".tracker").html();

        var elem = $(this).find("a.view_link");
        $(elem).text("View results");
        $(elem).attr("href", "/admin/results?utf8=✓&q%5Btracker_eq%5D="+ tracker +"&commit=Filter");

        $("th:contains('Tracker')").hide();
        $("td.tracker").hide();
      });
   }

    // -- Scan results
    $("td.is_false:contains('false')").html("<input type='checkbox' class='result_is_false' />");
    $("td.is_false:contains('true')").html("✓");

    $('td.is_false').click(function(){
        if($(this).html() != '✓')
        {
          if(confirm("Mark as false positive?"))
          {
            $(this).parent().removeClass("odd");
            $(this).parent().removeClass("even");
            $(this).parent().css({"background-color":"#e59895"});
            var elem = $(this).parent();

            var elem_id = $(elem).attr("id");
                elem_id = elem_id.substr(7);

             $.get('/admin/set_result_as_false/' + elem_id, function(data){
                setTimeout(function(){$(elem).hide();}, 1500);
              });
          }
          else
          {
            $("td.is_false").html("<input type='checkbox' class='result_is_false' />");
          }
        }
    });

   $("#scan_submit").click(function(){
   	
   		if(document.console_on)
   		{
   			return;
   		}
   		else
   		{
           var error;

           $('#scan_scan_name').css({"border":"1px solid #99A2AA"});
           $('#scan_profile').css({"border":"1px solid #99A2AA"});
           $('#scan_system').css({"border":"1px solid #99A2AA"});

           if($('#scan_scan_name').val() == '')
           {
              $('#scan_scan_name').css({"border":"solid 1px #932419"});
              error = true;
           }

           if($('#scan_profile').val() == '')
           {
              $('#scan_profile').css({"border":"solid 1px #932419"});
              error = true;
           }

           if($('#scan_system').val() == '')
           {
              $('#scan_system').css({"border":"solid 1px #932419"});
              error = true;
           }

           if(!error)
           {
             document.console_on = true;
             
             $("#scan_alert").fancybox({
                  maxWidth	: 200,
                  maxHeight	: 150,
                  fitToView	: false,
                  autoSize	: false,
                  closeClick	: false,
                  openEffect	: 'none',
                  closeEffect	: 'none',
                  modal       : true,
                  beforeClose	: function(){
                      window.location.href = "/admin/systems?utf8=✓&q%5Btracker_contains%5D="+ document.tracker +"&commit=Filter&order=id_desc"
                  }
             }).trigger('click');
             
             $("#fancybox-overlay").css({"opacity":"0.5"});	
             $("#scan_alert").show();

             document.profile_id = $('#scan_profile').val();

              setInterval(function(){
                  $.get('/admin/get_last_scan/', function(data){
                    document.tracker = data
                  });

                  $.get('/admin/get_logs/' + document.profile_id, function(data){
                      response = data.substr(0, 4);
                   
                      if(response == 'Done')
                      {
                        $('#scan_alert_msg').html('');

                        $('#scan_alert_msg').html('Finishing.. wait..');
                        setTimeout(function(){$.fancybox.close();}, 2500);
                      }
                      else if(response == 'Coul')
                      {
                        $('#scan_alert_msg').html('');

                        $('#scan_alert_msg').html('Finishing with errors.. wait..');
                        setTimeout(function(){$.fancybox.close();}, 2500);
                      }
                  });
              }, 3000);
          }
          else
          {
              return;
          }
		}
   });
});


